<div id="packagingSettings" style="height:100%">
    <div class="ui-layout-west">
        <div class="ui-layout-center">
            <div class="clearfix tbl speedExceptions" id="speedExceptions">Left</div>
        </div>
        <div class="ui-layout-south">
            <button class="small" id="sExceptionsAdd">Add new</button>    
            <button class="small" id="sExceptionsDelete">Delete</button>    
            <button class="small" id="sExceptionsSave">Save</button>    
            <button class="small" id="sExceptionsCancel">Cancel</button>
        </div>
    </div>
    <div class="ui-layout-center" id="settings">
    	<div class="ui-layout-north">
    		<div class="clearfix tbl unitTable" id="unitTable">North</div>
            <div class = "editButtons">
                <button class="small" id="uTableAdd">Add new</button>    
                <button class="small" id="uTableDelete">Delete</button>    
                <button class="small" id="uTableSave">Save</button>    
                <button class="small" id="uTableCancel">Cancel</button>
            </div>
    	</div>
    	<div class="ui-layout-center">
    		<div class="clearfix tbl lineDefaultSpeed" id="lineDefaultSpeed">Center</div>
            <div class = "editButtons">
                <button class="small" id="lDefSpeedsAdd">Add new</button>    
                <button class="small" id="lDefSpeedsDelete">Delete</button>    
                <button class="small" id="lDefSpeedsSave">Save</button>    
                <button class="small" id="lDefSpeedsCancel">Cancel</button>
            </div>
    	</div>
    	<div class="ui-layout-south">
    		<div class="clearfix tbl lineMixingCost" id="lineMixingCost">South</div>
            <div class = "editButtons">
                <button class="small" id="lMixCostAdd">Add new</button>    
                <button class="small" id="lMixCostDelete">Delete</button>    
                <button class="small" id="lMixCostSave">Save</button>    
                <button class="small" id="lMixCostCancel">Cancel</button>
            </div>
    	</div>
    </div>
</div>
<script>
    var packagingSettingsFX = function() {
        var _self = this;
        _self.perform = function() {
            //dodavanje layout-a za parent div
            $("#packagingSettings").layout({
                resizable: false,
                closable: false,
                west__size: ".5"

            });
            $("#settings").layout({
                north__size: "30%",
                center_size: "30%",
				south__size: "30%"
            });
            $(".ui-layout-west").layout({
                resizable: false,
                closable: false,
                south__size: "50"
            });
            $("#speedExceptions").addScroll();
            _self.loadDataForSpeedExceptions();

            $("#unitTable").addScroll();
            _self.loadDataForUnitTable();

            $("#lineDefaultSpeed").addScroll();
            _self.loadDataForLineDefaultSpeed();

            $("#lineMixingCost").addScroll();
            _self.loadDataForLineMixingCost();

            window.refreshAndSortTable = _self.refreshSortedTable;

        };
        
        _self.refreshSortedTable = function(table, columnName, ascDesc, callback){
            _self.table = table;

            if(table == "speedExceptions")
                _self.request = 270;
            else if(table == "unitTable")
                _self.request = 271;
            else if(table == "lineDefaultSpeed")
                _self.request = 272;
            else if(table == "lineMixingCost")
                _self.request = 273;

            _self.columnName = columnName;
            _self.sortItem = "";
            _self.ascDesc = ascDesc;
            _self.action = "getItems";

            _self.random = Math.random();
            _self.url = "js/tableParams/" + _self.request + ".json?x=" + random;

            $.get(_self.url, function(response) {
                $(response.desc).each(function(index,value){
                    if(value.title == columnName){
                        _self.sortItem = value.param;
                        return false;
                    }
                });

                var params = {
                    request: _self.request,
                    action: _self.action,
                    sortItem: _self.sortItem,
                    ascDesc: _self.ascDesc
                };

                window.tableConstructor.generateTable(params, callback);
            });

        };

        _self.loadDataForSpeedExceptions = function(callback) {
            var params = {
                request: 270,
                action: "getItems"
            };
            window.tableConstructor.generateTable(params, callback);
        };
        _self.loadDataForUnitTable = function(callback) {
            var params = {
                request: 271,
                action: "getItems"
            };
            window.tableConstructor.generateTable(params, callback);
        };
        _self.loadDataForLineDefaultSpeed = function(callback) {
            var params = {
                request: 272,
                action: "getItems"
            };
            window.tableConstructor.generateTable(params, callback);
        };
        _self.loadDataForLineMixingCost = function(callback) {
            var params = {
                request: 273,
                action: "getItems"
            };
            window.tableConstructor.generateTable(params, callback);
        };
        _self.assignEventHandlers = function(){
            $(document).on("click", ".scrollable .tbl.speedExceptions .row.data", function() {
                window.backendInterface.editItem($('.scrollable .tbl.speedExceptions'),$(this),_self.loadDataForSpeedExceptions,270);
            });
            $(document).on("click", "#sExceptionsAdd", function() {
                window.backendInterface.addItem($('.scrollable .tbl.speedExceptions'),_self.loadDataForSpeedExceptions,270);
            });
            $(document).on("click", "#sExceptionsDelete", function() {
                window.backendInterface.deleteItem(270);
            });
            $(document).on("click", "#sExceptionsSave", function() {
                window.backendInterface.save(270);
            });
            $(document).on("click", "#sExceptionsCancel", function() {
                window.backendInterface.cancel(270);
            });

            $(document).on("click", ".scrollable .tbl.unitTable .row.data", function() {
                window.backendInterface.editItem($('.scrollable .tbl.unitTable'),$(this),_self.loadDataForUnitTable,271);
            });
            $(document).on("click", "#uTableAdd", function() {
                window.backendInterface.addItem($('.scrollable .tbl.unitTable'),_self.loadDataForUnitTable,271);
            });
            $(document).on("click", "#uTableDelete", function() {
                window.backendInterface.deleteItem(271);
            });
            $(document).on("click", "#uTableSave", function() {
                window.backendInterface.save(271);
            });
            $(document).on("click", "#uTableCancel", function() {
                window.backendInterface.cancel(271);
            });


            $(document).on("click", ".scrollable .tbl.lineDefaultSpeed .row.data", function() {
                window.backendInterface.editItem($('.scrollable .tbl.lineDefaultSpeed'),$(this),_self.loadDataForLineDefaultSpeed,272);
            });
            $(document).on("click", "#lDefSpeedsAdd", function() {
                window.backendInterface.addItem($('.scrollable .tbl.lineDefaultSpeed'),_self.loadDataForLineDefaultSpeed,272);
            });
            $(document).on("click", "#lDefSpeedsDelete", function() {
                window.backendInterface.deleteItem(272);
            });
            $(document).on("click", "#lDefSpeedsSave", function() {
                window.backendInterface.save(272);
            });
            $(document).on("click", "#lDefSpeedsCancel", function() {
                window.backendInterface.cancel(272);
            });


            $(document).on("click", ".scrollable .tbl.lineMixingCost .row.data", function() {
                window.backendInterface.editItem($('.scrollable .tbl.lineMixingCost'),$(this),_self.loadDataForLineMixingCost,273);
            });
            $(document).on("click", "#lMixCostAdd", function() {
                window.backendInterface.addItem($('.scrollable .tbl.lineMixingCost'),_self.loadDataForLineMixingCost,273);
            });
            $(document).on("click", "#lMixCostDelete", function() {
                window.backendInterface.deleteItem(273);
            });
            $(document).on("click", "#lMixCostSave", function() {
                window.backendInterface.save(273);
            });
            $(document).on("click", "#lMixCostCancel", function() {
                window.backendInterface.cancel(273);
            });
        };
        _self.clearEventHandlers = function(){
            $(document).off("click", ".scrollable .tbl.speedExceptions .row.data");
            $(document).off("click", "#sExceptionsAdd");
            $(document).off("click", "#sExceptionsDelete");
            $(document).off("click", "#sExceptionsSave");
            $(document).off("click", "#sExceptionsCancel");

            $(document).off("click", ".scrollable .tbl.unitTable .row.data");
            $(document).off("click", "#uTableAdd");
            $(document).off("click", "#uTableDelete");
            $(document).off("click", "#uTableSave");
            $(document).off("click", "#uTableCancel");

            $(document).off("click", ".scrollable .tbl.lineDefaultSpeed .row.data");
            $(document).off("click", "#lDefSpeedsAdd");
            $(document).off("click", "#lDefSpeedsDelete");
            $(document).off("click", "#lDefSpeedsSave");
            $(document).off("click", "#lDefSpeedsCancel");

            $(document).off("click", ".scrollable .tbl.lineMixingCost .row.data");
            $(document).off("click", "#lMixCostAdd");
            $(document).off("click", "#lMixCostDelete");
            $(document).off("click", "#lMixCostSave");
            $(document).off("click", "#lMixCostCancel");
        };
    };
    if(!window.pSettingsFX){
        window.pSettingsFX = new packagingSettingsFX();
        window.pSettingsFX.perform();
        window.pSettingsFX.clearEventHandlers();
        window.pSettingsFX.assignEventHandlers();
    }
    else{
        window.pSettingsFX.perform();
        window.pSettingsFX.clearEventHandlers();
        window.pSettingsFX.assignEventHandlers();
    }
</script>